<template>
  <div style="text-align: center">
    <canvas id="background" :height="h" :width="w"></canvas>
    <el-form class="loginForm" label-width="80px">
      <h2>后台管理系统</h2>
      <el-form-item label="用户名：">
        <el-input v-model="userName"
                  autofocus
                  placeholder="请输入用户名"
                  @input="onInput"
                  @keydown.enter.native="login"
                  prefix-icon="el-icon-menu"></el-input>
      </el-form-item>
      <el-form-item label="密码：">
        <el-input v-model="password"
                  type="password"
                  placeholder="请输入密码"
                  @input="onInput"
                  @keydown.enter.native="login"
                  prefix-icon="el-icon-menu"></el-input>
      </el-form-item>
      <div style="color: red;height: 20px;margin-bottom: 22px">{{ info }}</div>
      <el-button type="success" style="width: 75%;" @click="login" :loading="vLoading" >登录</el-button>
    </el-form>
  </div>
</template>

<script>
import cookie from '../util/cookie'
export default {
  name: 'login',
  data () {
    return {
      colors: ['red', '#2AC845', 'orange', '#F08', 'darkgreen', 'darkorange'],
      canvas: document.querySelector('#background'),
      ctx: null,
      arcs: [],
      w: 0,
      h: 0,
      timer: null,
      userName: '',
      password: '',
      info: '',
      vLoading: false
    }
  },
  methods: {
    login: function () {
      this.vLoading = true
      var that = this
      this.$axios({
        method: 'post',
        url: '/api/userserver/login',
        data: {
          loginName: this.userName,
          password: this.password
        }
      }).then(function (resp) {
        console.log(resp)
        if (resp.data.code !== 200) {
          that.vLoading = false
          that.info = resp.data.message
        } else {
          cookie.set('yihong.Authorization', resp.data.data.Authorization, 1800)
          cookie.set('yihong.userInfo', JSON.stringify(resp.data.data.userInfo), 1800)
          that.vLoading = false
          that.$router.push('/layout')
        }
      }).catch(function (err) {
        console.log(err)
      })
    },
    onInput: function () {
      this.info = ''
    },
    getArc: function () {
      let o = {}
      // 半径
      o.r = this.random(5, 10)
      // 横坐标
      o.x = this.random(o.r, this.w - o.r)
      // 纵坐标
      o.y = this.random(this.h - o.r, this.r)
      // 横向速度方向
      o.vx = this.random(-2, 2, true)
      // 纵向速度方向
      o.vy = this.random(-2, 2, true)
      // 颜色
      o.color = this.colors[this.random(0, 5)]
      return o
    },
    // 移动
    moveArc: function (o) {
      if (o.x <= o.r || this.w - o.x <= o.r) {
        o.vx = -o.vx
      }
      if (o.y <= o.r || this.h - o.y <= o.r) {
        o.vy = -o.vy
      }
      o.x += o.vx
      o.y += o.vy
    },
    // 绘制
    draw: function (o) {
      this.ctx.beginPath()
      this.ctx.fillStyle = this.ctx.strokeStyle = o.color
      this.ctx.arc(o.x, o.y, o.r, 0, 2 * Math.PI)
      this.ctx.fill()
      this.ctx.closePath()
    },
    // 产生随机数,如果zeor参数为true则不允许为0
    random: function (min = 0, max = 0, nozeor) {
      if (max < min) {
        let temp = max
        max = min
        min = temp
      }
      let r = (Math.floor(Math.random() * ((max - min) + 1) + min))
      if (nozeor) {
        while (r === 0) {
          r = (Math.floor(Math.random() * ((max - min) + 1) + min))
        }
      }
      return r
    },
    init: function () {
      for (let i = 0; i < 521; i++) {
        let o = this.getArc()
        this.draw(o)
        this.arcs.push(o)
      }
    },
    refreshBackgound: function () {
      this.ctx.clearRect(0, 0, this.w, this.h)
      for (let item of this.arcs) {
        this.moveArc(item)
        this.draw(item)
      }
    },
    createTimer: function () {
      if (this.arcs.length < 521) {
        this.init()
      }
      if (!this.timer) {
        this.timer = setInterval(this.refreshBackgound, 100 / 4)
      }
    }
  },
  mounted: function () {
    if (this.$router.currentRoute.query.msg) {
      this.info = this.$router.currentRoute.query.msg
    }
    this.w = window.innerWidth
    this.h = window.innerHeight
    var that = this
    window.addEventListener('resize', () => {
      that.w = window.innerWidth
      that.h = window.innerHeight
      that.refreshBackgound()
    })
    this.canvas = document.querySelector('#background')
    this.ctx = this.canvas.getContext('2d')
    this.createTimer()
  },
  destroyed: function () {
    clearInterval(this.timer)
  }
}
</script>

<style scoped>
  #background {
    background-color: #FFFFFF;
    z-index: -1;
    position : absolute;
    display: block;
    margin: 0 auto;
  }
  .loginForm {
    border-radius: 10px;
    margin: 300px 0px 0px 0px;
    opacity: 0.9;
    background: linear-gradient(45deg, #EEEEEE, #AAAAAA, #FFFFFF);
    padding: 10px;
    display: inline-block;
    width: 400px;
    height: 300px;
  }
  .loginForm * {
    opacity: 1;
  }
</style>
